<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class RankList extends AdminController{
	public $_validation = null;
	function __construct(){
		parent::__construct();
		$this->load->model('MRankList');
		$this->_validation = Array();
	}

	/**
     *  排行
	 */
	function index(){
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		
		$this->formfilter->addFilter('id', 'where');
		$this->formfilter->addFilter('account','where');
		$this->formfilter->addFilter('status','where');
		
		$limit = $this->pagination($this->MRankList->getCount());
		$list = $this->MRankList->getList($limit);

		$this->assign('list', $list);
		$this->layout();
	}

	function modifyrow(){
	    $retObj = new stdClass();
	    $data = $this->input->post();
	    $id = (int)$data['id'];
	    if($id<1 || $id>10){
	        $retObj->code = -1;
	        $retObj->message = '要修改的记录id='.$id.'不存在';
	        echo json_encode($retObj);
	        exit();
	    }
	    if($id==1){
	        $nextItem = $this->MRankList->getOne(array('id'=>$id+1));
	        if($nextItem->friend_num > $data['friend_num']){
	            $retObj->code = -2;
	            $retObj->message = '要修改的记录的好友数量不能小于下一名的好友数量！';
	            echo json_encode($retObj);
	            exit();
	        }
	        
	    }else if($id==10){
	        $beforeItem = $this->MRankList->getOne(array('id'=>$id-1));
	        if($beforeItem->friend_num < $data['friend_num']){
	            $retObj->code = -3;
	            $retObj->message = '要修改的记录的好友数量不能大于前一名的好友数量！';
	            echo json_encode($retObj);
	            exit();
	        }
	        
	    }else{
	        $beforeItem = $this->MRankList->getOne(array('id'=>$id-1));
	        $nextItem = $this->MRankList->getOne(array('id'=>$id+1));
	        if($nextItem->friend_num > $data['friend_num']){
	            $retObj->code = -2;
	            $retObj->message = '要修改的记录的好友数量不能小于下一名的好友数量！';
	            echo json_encode($retObj);
	            exit();
	        }
	        if($beforeItem->friend_num < $data['friend_num']){
	            $retObj->code = -3;
	            $retObj->message = '要修改的记录的好友数量不能大于前一名的好友数量！';
	            echo json_encode($retObj);
	            exit();
	        }
	    }
	    $item = $this->MRankList->getOne(array('id'=>$id));
	    $item->phone = $data['phone'];
	    $item->friend_num = $data['friend_num'];
	    $item->awards_num = $data['awards_num'];
	    if($this->MRankList->update($item)){
	        $retObj->code = 1;
	        echo json_encode($retObj);
	        exit();
	    }else{
	        $retObj->code = -4;
	        $retObj->message = '修改失败或未进行修改';
	        echo json_encode($retObj);
	        exit();
	    }
	    
	}
}